#coding=utf-8
import os
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.patches as mpatches
import csv
import plotly.graph_objects as go
from matplotlib.animation import FuncAnimation
import re
from pylab import *

def IsSubString(SubStrList,Str):
    flag=true
    for substr in SubStrList:
        if not(substr in Str):
            flag=false
    return flag
 
def GetFileList(FindPath,FlageStr):
    FileList=[]
    FileNames=os.listdir(FindPath)
    for fn in FileNames:
         if (IsSubString(FlagStr,fn)):
             fullfilename=os.path.join(FindPath,fn)
             FileList.append(fullfilename)
    return FileList

def drawpic(filepath):
    #totalCount = re.sub("\D"," ",filepath)
    #print(totalCount)
    #totalCount = totalCount.split()
    #print(totalCount)
    #n = len(totalCount)
    #print(int(totalCount[a-1]))

    file = open(filepath)
    a = file.read()
    b = a.split('\n')
    for i in range(len(b)):
        b[i] = b[i].split()

    b = np.array(b)
    data = np.zeros((100,100),dtype=float)
    row = int(int(b[0][0])/100)
    #print(b)

    for i in range(int(b[0][2])):
        j = int(b[i+1][0])
        k = int(b[i+1][1])

        if j % row != 0:
            r = int(j/row)
        else:
            r = int(j/row-1)

        if k % row != 0:
            c = int(k/row)
        else:
            c = int(k/row-1)
        
        if r<100:
            if c<100:
                # data[r][c] = data[r][c]+float(b[0+1][2])
                 data[r][c]=1
    #data = np.array(data)
    #print(data)

    #x = 200*int(totalCount[n-2])/int(totalCount[n-1])
    #plot([x,x],[0,200],color='black',linewidth=1.0)
    #plot([0,200],[x,x],color='black',linewidth=1.0)

    norm = cm.colors.Normalize(vmin=-10,vmax=10)
    plt.imshow(data,cmap=cm.seismic,norm=norm)
    plt.colorbar()
    figname = filepath+'.png'
    plt.savefig(figname)
    plt.clf()
    #plt.show()

FindPath = '/home/txt/TXT_AMG/GPU/v3.14/Strong Matrix' 
FlagStr = ['mtx'] 
FileList = GetFileList(FindPath,FlagStr)
findex = 0
for fn in FileList:
    findex = findex+1
    if(os.path.isfile(fn)):
        print(str(findex)+'--->'+fn)
        drawpic(fn)
        

